<?php
/*
 * Created on Oct 11, 2011
 */
 class AdminLog_Model extends CI_Model{
	function AdminLog_Model(){
		parent::__construct();
		$this->load->helper('url');
	}
	 function saveLog($title,$content){
	 	$content = $content;
	 	$content = str_replace("<p>","",$content);
	 	$content = str_replace("</p>","<br/><br/>",$content);
		$data = array(
				'title'=>$title,
				'content'=>$content);
		$this->db->set("errordate",'NOW()',false);	
		$this->db->insert('adminlogerrors',$data);
		return $this->db->insert_id();
	}
	function searchAdminLogSubmit($fromDate,$toDate,$startIndex,$endIndex,$key,$errorcode){
		$strSQL = "select al.id,al.title,al.content,DATE_FORMAT(al.errordate,'%c/%d/%Y %T') as errordate from adminlogerrors al where true ";
		if($fromDate!=0){
			$strSQL .= " and al.errordate >='".$fromDate."' ";	
		}
		if($toDate!=0){
			$strSQL .= " and al.errordate < ADDDATE('".$toDate."',1) ";				
		}
		if(isset($key)&&$key!=""){
			$strSQL .= " and (al.title like '%".mysql_real_escape_string($key)."%') or (al.content like '%".mysql_real_escape_string($key)."%') ";
		} 
		if(isset($errorcode)&&$errorcode!="") $strSQL .= " and id = ".$errorcode;
		$strSQL .= " order by al.errordate desc, al.id desc ";	
		
		if($endIndex>0){
		$strSQL .= " limit ".$startIndex.",".($endIndex-$startIndex);
		}
		$result=$this->db->query($strSQL);
		return $result->result();
	}
	function getTotalResults($fromDate,$toDate,$key,$errorcode){
		//return count($this->searchAdminLogSubmit($fromDate,$toDate,0,0,$key));
		$strSQL = "select count(id) as count_log from adminlogerrors al where true";
		if($fromDate!=0){
			$strSQL .= " and al.errordate >='".$fromDate."'";
		}
		if($toDate!=0){
			$strSQL .= " and al.errordate < ADDDATE('".$toDate."',1)";
		}
		if(isset($key)&&$key!=""){
			$strSQL .= "and (al.title like '%".mysql_real_escape_string($key)."%') or (al.content like '%".mysql_real_escape_string($key)."%') ";
		}
		if(isset($errorcode)&&$errorcode!="") $strSQL .= " and id = ".$errorcode;
		$result=$this->db->query($strSQL);
		if(isset($result) && !empty($result))
		{
			$logs=$result->first_row();
			if(isset($logs) && !empty($logs))
			$total=$logs->count_log;
		}
		return $total;
	}
	
	function delete_logs($listIds)
	{
		
		$this->load->database();
		$strSQL="delete from adminlogerrors where id in (".$listIds.")";
		//echo($strSQL);
		$result=$this->db->query($strSQL);
		return $result;	
	}
 }
?>
